package org.wbd.pklib.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import org.wbd.pklib.db.Book;
import org.wbd.pklib.db.Item;
import org.wbd.pklib.db.Repo;
import org.wbd.pklib.db.User;
import org.wbd.pklib.db.logic.BookLogic;
import org.wbd.pklib.db.logic.LogicRespond;
import org.wbd.pklib.db.logic.RepoLogic;

public class BookBean {
	private String isbn;
	private String author;
	private String title;
	private String publisher;
	private Integer publicationYear;
	private String categories;
	private ArrayList<Item> myBooks;
	private Item selectedBook;
	private ArrayList<Book> result;
	private Book bookToAdd;
	private Repo selectedRepo;
	private User user;
	private Book book= new Book();

	
	private List<String> categoryList;
	
	public BookBean(){
		FacesContext.getCurrentInstance().getExternalContext().getFlash().clear();
		categories="";
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public Integer getPublicationYear() {
		return publicationYear;
	}
	public void setPublicationYear(Integer publicationYear) {
		this.publicationYear = publicationYear;
	}
	public String getCategories() {
		return categories;
	}
	public void setCategories(String categories) {
		this.categories = categories;
	}	
	
	public List<String> getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List<String> categoryList) {
		this.categoryList = categoryList;
		
		for (String cat : categoryList){		
			categories += cat + ":" ;
		}
		
	}
	
	public ArrayList<Book> getResult() {
		return result;
	}
	public void setResult(ArrayList<Book> result) {
		this.result = result;
	}
	public ArrayList<Item> getMyBooks() {
		return myBooks;
	}
	public void setMyBooks(ArrayList<Item> myBooks) {
		this.myBooks = myBooks;
	}
	public Item getSelectedBook() {
		return selectedBook;
	}
	public void setSelectedBook(Item selectedBook) {
		this.selectedBook = selectedBook;
	}
	
	public ArrayList<Repo> getRepoList() {
		UserBean bean= (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userBean");
		return bean.getUserRepoList();
	}
	

	public void setSelectedRepo(Repo selectedRepo) {
		this.selectedRepo = selectedRepo;
	}
	public void setBookToAdd(Book bookToAdd) {
		this.bookToAdd = bookToAdd;
	}
	
	
	
	public Repo getSelectedRepo() {
		return selectedRepo;
	}
	public void setUser(User user) {
		this.user = user;
	}
		
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public String addBook(){
		BookLogic logic= BookLogic.getInstance();
		book.setCategories(categories);
		LogicRespond respond= logic.addBook(book);	
		FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Book added sucessful!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		} else {
			FacesMessage message = new FacesMessage("Book adding failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "MyPage?faces-redirect= true";
			
	}
	
	public String joinBook(){
		RepoLogic logic= RepoLogic.getInstance();
		
		LogicRespond respond= logic.joinBook(user, selectedRepo, bookToAdd);
		if (respond.isSuccesful()) {
			FacesMessage message = new FacesMessage("Book added sucessful!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		} else {
			FacesMessage message = new FacesMessage("Book adding failed!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}
		return "menu?faces-redirect= true";
	}
	
}
